{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n双端队列\\n    可以再头部和尾部进行添加和删除元素\\n\\n\\n用list或者链表实现\\n\\nDeques 是允许类似栈和队列的混合行为的数据结构。\\ndeque 的基本操作是 addFront，addRear，removeFront，removeRear 和 isEmpty。\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "双端队列\n",
    "    可以再头部和尾部进行添加和删除元素\n",
    "\n",
    "\n",
    "用list或者链表实现\n",
    "\n",
    "Deques 是允许类似栈和队列的混合行为的数据结构。\n",
    "deque 的基本操作是 addFront，addRear，removeFront，removeRear 和 isEmpty。\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "0\n",
      "2\n",
      "1\n",
      "dog\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "class Deque(object):\n",
    "    def __init__(self):\n",
    "        self.items = []\n",
    "        \n",
    "    def isEmpty(self):\n",
    "        return self.items == []\n",
    "\n",
    "    def addFront(self,item):\n",
    "        \"\"\" 双端队列的头部是操作list的尾部 \"\"\"\n",
    "        self.items.append(item)\n",
    "        \n",
    "    def addRear(self,item):\n",
    "        self.items.insert(0,item)\n",
    "    \n",
    "    def removeFront(self,):\n",
    "        return self.items.pop()\n",
    "        \n",
    "    def removeRear(self):\n",
    "        return self.items.pop(0)\n",
    "        \n",
    "    def size(self):\n",
    "        return len(self.items)\n",
    "    \n",
    "    def travel(self):\n",
    "        for i in self.items:\n",
    "            print(i)\n",
    "    \n",
    "if __name__ == \"__main__\":\n",
    "    d = Deque()\n",
    "    print(d.isEmpty())\n",
    "    print(d.size())\n",
    "    d.addFront(1)\n",
    "    d.addRear('dog')\n",
    "    print(d.size())\n",
    "    print(d.removeFront())\n",
    "    d.travel()\n",
    "    print(d.size())\n",
    "    d.removeFront()\n",
    "    d.travel()\n",
    "    print(d.size())\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "回文检查\n",
    "    回文字符串：首尾遍历相同。\n",
    "    比如：radar toot madam\n",
    "\"\"\"\n",
    "def check_pal(str):\n",
    "    dq = Deque()\n",
    "    \n",
    "    for i in str:\n",
    "        dq.addFront(i)\n",
    "    \n",
    "    while dq.size() > 1:\n",
    "        head = dq.removeFront()\n",
    "        rear = dq.removeRear()\n",
    "        if head is not rear:\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    print(check_pal('toot'))\n",
    "    print(check_pal('abc'))\n",
    "    print(check_pal(\"lsdkjfskf\"))\n",
    "    print(check_pal(\"radar\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
